﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
using System.Collections;

namespace TP2
{
    public partial class AddAccumulationForm : Form
    {      
        string client_type;
        int id_client;
        AccumulationForm accumulationForm;
        public AddAccumulationForm(string clientType,AccumulationForm Fr,int idClient)
        {
            id_client = idClient;
            client_type = clientType;
            accumulationForm = Fr;
            InitializeComponent();
        }

        private void SelectFromAccumulation(string client_type)
        {
            DBWorkClass dbWork = new DBWorkClass();
            ClassRateType selectRate = new ClassRateType();
            selectRate = dbWork.SelectFromAccumulationRate(client_type);
            if (selectRate.id_rate.Count == 0) { dataGridView1.RowCount = 1; } else { dataGridView1.RowCount = selectRate.id_rate.Count; }
            int j = 0;
            while (j < selectRate.id_rate.Count)
            {
                dataGridView1[0, j].Value = selectRate.title[j].ToString();
                dataGridView1[1, j].Value = selectRate.procent[j].ToString();
                dataGridView1[2, j].Value = selectRate.period[j].ToString();
                j++;
            }          
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddAccumulationForm_Load(object sender, EventArgs e)
        {
            this.SelectFromAccumulation(client_type);
            
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            try
            {
                DBWorkClass dbWork = new DBWorkClass();
                int index;
                index = dataGridView1.CurrentRow.Index;
                int period = Convert.ToInt32(dataGridView1[2, index].Value);
                Double procent = Convert.ToDouble(dataGridView1[1, index].Value);
                int deposit = Convert.ToInt32(textBoxDeposit.Text);
                dbWork.InsertToAccumulation(client_type, dateTimePicker1.Value, id_client, period, procent, deposit);
                accumulationForm.RefreshTable();
                this.Close();
            }
            catch (FormatException) { MessageBox.Show("ошибка в данных!"); }
        }

        private void buttonCalc_Click(object sender, EventArgs e)
        {
            try
            {
                int index = dataGridView1.CurrentRow.Index;
                Double procent = Convert.ToDouble(dataGridView1[1, index].Value);
                int period = Convert.ToInt32(dataGridView1[2, index].Value);
                int deposit = Convert.ToInt32(textBoxDeposit.Text);
                LogicClass logicOb = new LogicClass();
                labelItog.Text = logicOb.UpdateDeposit(deposit, period, procent).ToString();
            }
            catch (FormatException) { MessageBox.Show("ошибка в данных!"); }
        }
    }
}
